package com.sskj.mine;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.hjq.toast.ToastUtils;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.HideUtil;
import com.sskj.mine.component.DaggerUserDataComponent;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * @author Hey
 * Create at  2019/05/01
 */
@Route(path = RConfig.MINE_SET_PAY_PWD)
public class SettingPasswordActivity extends BaseActivity<SettingPasswordPresenter> {

    @BindView(R2.id.password_name)
    TextView passwordName;
    @BindView(R2.id.password_edt)
    EditText passwordEdt;
    @BindView(R2.id.password_name2)
    TextView passwordName2;
    @BindView(R2.id.password_edt2)
    EditText passwordEdt2;
    @BindView(R2.id.mobile_name)
    TextView mobileName;
    @BindView(R2.id.mobile_edt)
    EditText mobileEdt;
    @BindView(R2.id.code_edt)
    EditText codeEdt;
    @BindView(R2.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R2.id.submit_btn)
    Button submitBtn;
    @Inject
    UserViewModel userViewModel;

    /**
     * 0-> 修改登录密码
     * 1->设置交易密码
     * 2->修改交易密码
     */
    @Autowired
    int type=1;
    String mobile;
    String name;
    String name2;
    String hint;
    String hint2;
    private DisposableSubscriber<Long> disposableSubscriber;
    private UserData userData;

    @Override
    public int getLayoutId() {
        return R.layout.mine_activity_setting_password;
    }

    @Override
    public SettingPasswordPresenter getPresenter() {
        return new SettingPasswordPresenter();
    }

    @Override
    public void initView() {
        DaggerUserDataComponent.create().inject(this);

        ARouter.getInstance().inject(this);
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
                mobile = userData.getMobile();
                mobileEdt.setText(HideUtil.getPhoneHide(mobile));
            } else {
                userData = null;
            }
        });
        switch (type) {
            case 0:
                setTitle(getString(R.string.mine_settingPasswordActivity1));
                name = getString(R.string.mine_settingPasswordActivity2);
                name2 = getString(R.string.mine_settingPasswordActivity3);
                hint = getResources().getString(R.string.mine_login_pwd);
                hint2 = getResources().getString(R.string.mine_login_pwd);
                break;
            case 1:
                setTitle(getString(R.string.mine_settingPasswordActivity4));
                name = getString(R.string.mine_settingPasswordActivity5);
                name2 = getString(R.string.mine_settingPasswordActivity6);
                hint = getResources().getString(R.string.mine_trade_pwd);
                hint2 = getResources().getString(R.string.mine_trade_pwd);
                break;
            case 2:
                setTitle(getString(R.string.mine_settingPasswordActivity7));
                name = getString(R.string.mine_settingPasswordActivity5);
                name2 = getString(R.string.mine_settingPasswordActivity6);
                hint = getResources().getString(R.string.mine_trade_pwd);
                hint2 = getResources().getString(R.string.mine_trade_pwd);
                break;
        }
        mobileEdt.setText(HideUtil.getPhoneHide(mobile));
        passwordName.setText(name);
        passwordName2.setText(name2);
        passwordEdt.setHint(hint);
        passwordEdt2.setHint(hint2);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initEvent() {
        ClickUtil.click(getCodeTv, () -> {
            mPresenter.requestSms(mobile);
        });

        ClickUtil.click(submitBtn, () -> {
            if (isEmpty(passwordEdt)) {
                ToastUtils.show(hint);
                return;
            }
            if (isEmpty(passwordEdt2)) {
                ToastUtils.show(hint2);
                return;
            }
            if (!CheckUtil.isPayPwd(getText(passwordEdt))) {
                ToastUtils.show(getString(R.string.mine_addAccountActivity7));
                return;
            }
            if (!getText(passwordEdt).equals(getText(passwordEdt2))) {
                ToastUtils.show(getString(R.string.mine_addAccountActivity8));
                return;
            }

            if (isEmpty(codeEdt)) {
                ToastUtils.show(getString(R.string.mine_bindMobileActivity4));
                return;
            }
            mPresenter.setPayPwd(getText(passwordEdt), getText(codeEdt), 0 != type);
        });
    }

    public void onAuthCodeSuccess() {
        startCount();
    }

    public void startCount() {
        getCodeTv.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                if (getCodeTv != null) {
                    getCodeTv.setText(time + getString(R.string.mine_bindMobileActivity5));
                }
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                if (getCodeTv != null) {
                    getCodeTv.setText(getString(R.string.mine_withdrawActivity7));
                    getCodeTv.setEnabled(true);
                }
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }
}
